<?php

/**
 * @package Bike
 * @subpackage Serice
 */

/**
 * Class service locator
 * @package Bike
 * @subpackage Service
 */
class Bike_Service_Locator {

	/**
	 * Static storage
	 * @var array
	 */
	protected static $storage = array();

	/**
	 *
	 * Add service to storage
	 * @param string $name storage key
	 * @param string|object $serviceObject
	 * @return boolean
	 */
	public static function addService($name, $serviceObject){
		self::$storage[$name] = $serviceObject;
		return true;
	}

	/**
	 *
	 * Get object from storage by key
	 * @param string $name
	 * @return object
	 * @throws Bike_Service_Exception
	 */
	public static function getService($name){
		if(empty(self::$storage[$name])){
			throw new Bike_Service_Exception('Service ' . $name . ' not found', Bike_Service_Exception::SERVICE_NOT_FOUND);
		}
		if(is_string(self::$storage[$name])){
			self::$storage[$name] = new self::$storage[$name]();
		}
		return self::$storage[$name];
		
	}

	/**
	 *
	 * Remove service from storage by key
	 * @param string $name
	 */
	public static function removeService($name){
		if(isset(self::$storage[$name])){
			unset(self::$storage[$name]);
		}
		return true;
	}
}
